package com.bj.dz.service.impl;

import com.bj.dz.entity.SingleBuilding;
import com.bj.dz.mapper.SingleBuildingMapper;
import com.bj.dz.service.SingleBuildingService;
import com.bj.dz.util.Constants;
import com.bj.dz.util.EmptyUtils;
import com.bj.dz.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class SingleBuildingServiceImpl implements SingleBuildingService {

    @Resource
    private SingleBuildingMapper singleBuildingMapper;

    public SingleBuilding getSingleBuildingById(Integer id){
        return singleBuildingMapper.getSingleBuildingById(id);
    }

    public List<SingleBuilding>	getSingleBuildingListByMap(Map<String,Object> param){
        return singleBuildingMapper.getSingleBuildingListByMap(param);
    }

    public Integer getSingleBuildingCountByMap(Map<String,Object> param){
        return singleBuildingMapper.getSingleBuildingCountByMap(param);
    }

    @Transactional
    public boolean addSingleBuilding(SingleBuilding singleBuilding){
            return singleBuildingMapper.insertSingleBuilding(singleBuilding)>0?true:false;
    }

    @Transactional
    public boolean modifySingleBuilding(SingleBuilding singleBuilding){
        return singleBuildingMapper.updateSingleBuilding(singleBuilding)>0?true:false;
    }

    @Transactional
    public boolean deleteSingleBuildingById(Integer id){
        return singleBuildingMapper.deleteSingleBuildingById(id)>0?true:false;
    }

    public Page<SingleBuilding> querySingleBuildingPageByMap(Map<String,Object> param, Integer pageNo, Integer pageSize){
        Integer total = singleBuildingMapper.getSingleBuildingCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<SingleBuilding> singleBuildingList = singleBuildingMapper.getSingleBuildingListByMap(param);
        page.setRows(singleBuildingList);
        return page;
    }

}
